/*
 * Write a method to replace all spaces in a string with '%20'
 */
public class Chapter1_5 {
	public static void main(String args[])
	{
		Chapter1_5 test = new Chapter1_5();
		test.replaceSpace("abc edf");
		test.replaceSpace("abc ");
		test.replaceSpace("abc  efg");
		test.replaceSpace(" abc ");
	}
	public void replaceSpace(String str)
	{
		char ch[] = str.toCharArray();
		int spaceNum = 0;
		for(int i = 0; i < str.length(); i++)
		{
			if(ch[i]==' ')
				spaceNum++;
		}
		
		
		int newLength = str.length() + spaceNum * 2;
		char newCh[] = new char[newLength];
		newLength -= 1;
		for(int i = str.length() - 1; i >= 0; i--)
		{
			if(ch[i] == ' '){
				newCh[newLength] = '0';
				newCh[newLength - 1] = '2';
				newCh[newLength - 2] = '%';
				newLength -= 3;
			}else{
				newCh[newLength] = ch[i];
				newLength--;
			}
		}
		
		//print out result
		for(int i = 0; i < newCh.length; i++)
			System.out.print(newCh[i]);
		System.out.println();
	}
}
